package com.yoyo.schedule.task_manager.processor;

/**
 * 多个线程间进行通信(object,threadCount)
 */
public class ObjectLock {

    private Object object = new Object();
    private int threadCount = 0;

    public ObjectLock() {
    }

    //synchronized
    public void objectWait() throws Exception {
        synchronized (object) {
            this.object.wait();
        }
    }

    public void objectNotifyAll() throws Exception {
        synchronized (object) {
            this.object.notifyAll();
        }
    }
    //count

    public void increaseThreadCount1() {
        synchronized (this) {
            threadCount = threadCount + 1;
        }
    }

    public void reduceThreadCount1() {
        synchronized (this) {
            threadCount = threadCount - 1;
        }
    }

    //
    public boolean reduceThreadCount1IfThreadCountNotEqual1AndReturnTheCurrentThreadIsNotLastThread() {
        synchronized (this) {
            if (this.threadCount == 1) {
                return false;
            } else {
                threadCount = threadCount - 1;
                return true;
            }
        }
    }


}
